package com.qf.servlet;

import com.qf.entity.Pagebean;
import com.qf.entity.StuInfo;
import com.qf.service.Impl.StulnfoServiceImpl;
import com.qf.service.StulnfoService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author a1275872671
 * @version v1.0
 * @project guanqipeng
 * @package com.qf.servlet
 * @company
 * @date 2024/4/30 10:13
 */
@WebServlet(urlPatterns = "/StulnfoServlet")
public class StulnfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        int pn=1;
        int ps=10;
        try {
            if (pageNum!=null){
                pn=Integer.parseInt(pageNum);
                if (pn<1){
                    pn=1;
                }
            }
            if (pageSize!=null){
                ps=Integer.parseInt(pageSize);
                if (ps<1){
                    ps=10;
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        System.out.println(ps);
        StulnfoService stulnfoService=new StulnfoServiceImpl();
//       List<StuInfo> stuInfos=stulnfoService.finAll();
        Pagebean<StuInfo> pagebean=stulnfoService.findByPage(pn,ps);
       if (pagebean!=null){
           for (StuInfo stuInfo : pagebean.getData()) {
               System.out.println(stuInfo.toString());

           }
       }
        HttpSession session = request.getSession();
        StuInfo user = (StuInfo) session.getAttribute("login");
        if (user==null){
            response.sendRedirect("login.html");
            return;
        }
       request.setAttribute("pagebean",pagebean);
       request.getRequestDispatcher("/xinxi.jsp").forward(request,response);

    }
}
